﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.supply
{
    public partial class ListWarnInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                string  user_id = ds.Tables[0].Rows[0]["user_id"].ToString();

                Bind(user_id);
            }
        }

        private void Bind(string uid)
        {
            Maticsoft.BLL.warnInfo wm = new Maticsoft.BLL.warnInfo();

            DataSet ds = wm.GetListByView("");

            DataTable dt = ds.Tables[0];

            DataTable dt_source = dt.Clone();

            dt_source.Columns.Add("intime",typeof(string));
            dt_source.Columns.Add("warnStr",typeof(string));

            foreach (DataRow row in dt.Rows)
            {
                string material = row["material"].ToString();
                string userId = row["uid"].ToString();

                DataSet ds_intime = wm.GetListIntime(" material = '"+material+"' and uid="+userId );

                DataRow newRow = dt_source.NewRow();

                newRow["totalNums"] = row["totalNums"];
                newRow["uid"] = row["uid"];
                newRow["material"] = row["material"];
                newRow["user_name"] = row["user_name"];
                newRow["minNums"] = row["minNums"];
                newRow["maxNums"] = row["maxNums"];
                newRow["warnTime"] = row["warnTime"];
                newRow["intime"] = ds_intime.Tables[0].Rows[0]["intime"];

                dt_source.Rows.Add(newRow);

            }

            foreach (DataRow row in dt_source.Rows)
            {
                DateTime time = DateTime.Parse(row["intime"].ToString());//入库最早时间
                decimal minNums = decimal.Parse(row["minNums"].ToString());//最小数量
                decimal maxNums = decimal.Parse(row["maxNums"].ToString());//最大数量
                decimal totalNums = decimal.Parse(row["totalNums"].ToString());//总数量
                int warnTime = int.Parse(row["warnTime"].ToString());//报警时间



                string strMsg = "";

                if (totalNums < minNums)
                {
                    strMsg += " 最小量预警 ";
                }
                if (totalNums > maxNums)
                {
                    strMsg += " 最大量预警 ";
                }
                if (time.AddMonths(warnTime) < DateTime.Now)
                {
                    strMsg += " 库存超时 ";
                }
                row["warnStr"] = strMsg;
            }

            DataTable dt_sourceFilter = dt_source.Clone();

            foreach (DataRow row in dt_source.Rows)
            {
                if (!string.IsNullOrEmpty(row["warnStr"].ToString()))
                {
                    dt_sourceFilter.ImportRow(row);
                }
            }

            this.GridView1.DataSource = dt_sourceFilter;
            this.GridView1.DataBind();
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        //导出excel
        protected void btn_out_Click(object sender, EventArgs e)
        {
            this.GridView1.AllowPaging = false;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                string user_id = ds.Tables[0].Rows[0]["user_id"].ToString();

                Bind(user_id);
            }

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=wuliaoWARN.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>物料库存预警</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;
                //int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                string user_id = ds.Tables[0].Rows[0]["user_id"].ToString();

                Bind(user_id);
            }
        }
    }
}